<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: CreatePageLanguage.php 379 2010-12-21 10:05:19Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalPagecontentRecord');
Prado::using('Application.Common.Data.PortalPageRecord');
Prado::using('Application.Common.Data.PortalSiteMenuRecord');
Prado::using('Application.Common.Data.PortalSiteMenuItemRecord');
Prado::using('Application.Common.Data.PortalRoleRecord');

class CreatePageLanguage extends PortalSystemPage
{
	public function onInit($param)
	{
		parent::onInit($param);

		if(!$this->User)
		{
			$this->reportError(1,"You need to be logged in to use this page");
		}

		if($this->User->RoleId != $this->Application->Parameters['AdminRole'])
		{
			$this->reportError(1,"You need to have admin role to access this page: '" . $this->Service->RequestedPagePath . "'");
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->Title = $this->Application->Parameters['SiteTitle'] . " - " . Prado::localize("Create from language");
		
		if(!$this->IsPostBack)
		{
			$this->ddlToCulture->DataSource	= $this->DataAccess->createCommand("select * from tbllanguages where active=1")->query();
			$this->ddlToCulture->dataBind();

			$this->hfPageName->Value = $this->Request['pagename'];
		}
	}

	public function saveButtonClicked($sender,$param)
	{
		if($this->IsValid)
		{
			$pagename		= $this->hfPageName->Value;
			$fromlanguage	= $this->Application->Culture;
			$tolanguage		= $this->ddlToCulture->SelectedValue;

			$fromContent = PortalPagecontentRecord::finder()->findAllBySql("select * from tblpagecontent where pagename='$pagename' and culture='$fromlanguage'");
			foreach($fromContent as $source)
			{
				$newrec = new PortalPagecontentRecord();
				$newrec->pagename	= $pagename;
				$newrec->active		= 1;
				
				$newrec->controlname	= $source->controlname;
				$newrec->culture		= $tolanguage;
				$newrec->displayorder	= $source->displayorder;
				
				$newrec->needlogin		= $source->needlogin;
				$newrec->portlet		= $source->portlet;
				$newrec->role			= $source->role;
				$newrec->showafterlogin	= $source->showafterlogin;
				$newrec->save();
			}
		}
	}

}
?>